package com.loopy.jdbc.Jdbc01;

import com.mysql.jdbc.Driver;
import org.testng.annotations.Test;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcConn {
    @Test
   public void  Connection2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
       Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
       Driver driver = (Driver)aClass.newInstance();
       String url="jdbc:mysql://localhost:3306/testdb";
       Properties properties = new Properties();
       properties.setProperty("user","root");
       properties.setProperty("password","123456");
       Connection connect = driver.connect(url, properties);
       String sql="insert into actor values(null,'周星驰','男','1970-01-02','110')";
       Statement statement = connect.createStatement();
       int rows = statement.executeUpdate(sql);
       System.out.println(rows>0?"成功":"失败");
       statement.close();
       connect.close();
   }
@Test
    public void  Connection3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
    Driver driver = (Driver)aClass.newInstance();
    String url="jdbc:mysql://localhost:3306/testdb";
    String user="root";
    String password="123456";
    DriverManager.registerDriver(driver);
    Connection connection = DriverManager.getConnection(url, user, password);
    String sql="insert into actor values(null,'周星驰','男','1970-01-02','110')";
    Statement statement = connection.createStatement();
    int rows = statement.executeUpdate(sql);
    System.out.println(rows>0?"成功":"失败");
    statement.close();
    connection.close();

}
@Test
    public void  Connection4() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://localhost:3306/testdb";
    String user="root";
    String password="123456";
    Connection connection = DriverManager.getConnection(url, user, password);
    String sql="insert into actor values(null,'周星驰','男','1970-01-02','110')";
    Statement statement = connection.createStatement();
    int rows = statement.executeUpdate(sql);
    System.out.println(rows>0?"成功":"失败");
    statement.close();
    connection.close();
}
    @Test
    public void  Connection5() throws ClassNotFoundException, IOException, SQLException {

        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        Class.forName(driver);
        DriverManager.getConnection(url,user,password);
        System.out.println("输出方式5连接");
    }

    @Test
    public void Connection06() throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("connection="+connection);
        Statement statement = connection.createStatement();
//        String sql="create table news(id int primary key,content varchar(32))";
//        String sql="insert into news values(1,'aaa')";
//        String sql="update news set content='bbb' where id=1";
        String sql="delete from news where id=3";

        int i = statement.executeUpdate(sql);
        System.out.println(i>0?"成功":"失败");
        statement.close();
        connection.close();

    }

}
